/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VipManageList.java
 *
 * Created on 2009-12-5, 11:34:09
 */

package com.ordering.menu;

import com.ordering.dao.VipCustomerJpaController;
import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.VipCustomer;
import com.ordering.utils.WindowLocationUtils;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hua
 */
public class VipManageList extends javax.swing.JDialog {

    /** Creates new form VipManageList */
    public VipManageList(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        WindowLocationUtils.hideColumn(vipTable, 9);
        WindowLocationUtils.setWindowCenter(this);
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        vipTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        btnAddNew = new javax.swing.JMenu();
        btnEdit = new javax.swing.JMenu();
        btnDel = new javax.swing.JMenu();
        btnRefresh = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("会员管理");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        vipTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "卡号", "售卡日期", "姓名", "性别", "证件号码", "卡类型", "金额", "折扣", "联系电话", "序号"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        vipTable.setName("vipTable"); // NOI18N
        vipTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(vipTable);
        vipTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        vipTable.getColumnModel().getColumn(4).setPreferredWidth(120);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        btnAddNew.setText("新增");
        btnAddNew.setName("btnAddNew"); // NOI18N
        btnAddNew.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddNewMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnAddNew);

        btnEdit.setText("修改");
        btnEdit.setName("btnEdit"); // NOI18N
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnEdit);

        btnDel.setText("删除");
        btnDel.setName("btnDel"); // NOI18N
        btnDel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDelMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnDel);

        btnRefresh.setText("刷新");
        btnRefresh.setName("btnRefresh"); // NOI18N
        btnRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRefreshMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnRefresh);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 720, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddNewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddNewMouseClicked
         VipManageEdit editWin = new VipManageEdit();
         editWin.addWindowListener(new java.awt.event.WindowAdapter() {
             //监听编辑窗口关闭后刷新列表事件
                    public void windowClosed(java.awt.event.WindowEvent e) {
                        initTable();
                    }
                });
         editWin.setVisible(true);
    }//GEN-LAST:event_btnAddNewMouseClicked

    private void btnRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRefreshMouseClicked
          initTable();
    }//GEN-LAST:event_btnRefreshMouseClicked

    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEditMouseClicked
        int rowIndex = vipTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object vipId = ((DefaultTableModel)vipTable.getModel()).getValueAt(rowIndex,9);
           if(vipId!=null&&!"".equalsIgnoreCase(vipId.toString())){
                VipCustomerJpaController control = new VipCustomerJpaController();
                VipCustomer vip = control.findVipCustomer(Long.valueOf(vipId.toString()));
                VipManageEdit editWin = new VipManageEdit(vip);
                 editWin.addWindowListener(new java.awt.event.WindowAdapter() {
                     //监听编辑窗口关闭后刷新列表事件
                            public void windowClosed(java.awt.event.WindowEvent e) {
                                initTable();
                            }
                        });
                editWin.setVisible(true);
           }
        }
    }//GEN-LAST:event_btnEditMouseClicked

    private void btnDelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDelMouseClicked
          int rowIndex = vipTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object vipId = ((DefaultTableModel)vipTable.getModel()).getValueAt(rowIndex,9);
           if(vipId!=null&&!"".equalsIgnoreCase(vipId.toString())){
                VipCustomerJpaController control = new VipCustomerJpaController();
                try {
                    control.destroy(Long.valueOf(vipId.toString()));
                    ((DefaultTableModel)vipTable.getModel()).removeRow(rowIndex);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(VipManageList.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
        }
    }//GEN-LAST:event_btnDelMouseClicked

    /*
     * 初始化vipTable
    /**
    * @param args the command line arguments
    */
    public void initTable(){
        for(int rowIndex=vipTable.getRowCount()-1;rowIndex>-1;rowIndex--){
            ((DefaultTableModel)vipTable.getModel()).removeRow(rowIndex);
        }
        VipCustomerJpaController control = new VipCustomerJpaController();
        List<VipCustomer> list = control.findVipCustomerEntities();
        int cols = vipTable.getColumnCount();
        String[] rowData = new String[cols];
        for(VipCustomer vip : list){
            rowData[0]=vip.getCardNum();
            if(vip.getCardDate()!=null)
                rowData[1]=vip.getCardDate().toString();
            else
                rowData[1]="";
            rowData[2]=vip.getVipName();
            if(vip.getVipSex()!=null)
                rowData[3]=vip.getVipSex()==0?"男":"女";
            else
                rowData[3]="";
            rowData[4]=vip.getVipCred();
            if(vip.getCardType()!=null)
                switch(vip.getCardType()){
                    case 0 :
                        rowData[5]="普通会员" ;
                        break;
                    case 1 :
                        rowData[5]="VIP会员";
                        break;
                }
            else
                rowData[5]="";
            if(vip.getCardAmount()!=null)
                rowData[6]=vip.getCardAmount().toString();
            else
                rowData[6]="";
            if(vip.getCardDiscount()!=null)
                rowData[7]=vip.getCardDiscount().toString();
            else
                rowData[7]="";
            rowData[8]=vip.getVipPhone();
            if(vip.getVipId()!=null)
                rowData[9]=vip.getVipId().toString();
            else
                rowData[9]="";
            ((DefaultTableModel)vipTable.getModel()).addRow(rowData);
        }
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                   VipManageList dialog = new VipManageList(new javax.swing.JFrame(), false);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosed(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu btnAddNew;
    private javax.swing.JMenu btnDel;
    private javax.swing.JMenu btnEdit;
    private javax.swing.JMenu btnRefresh;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable vipTable;
    // End of variables declaration//GEN-END:variables

}
